package org.bbelovic.devel.exercises;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Permutations {

    public static Set<String> permutations(String s, String input) {
        final Set<String> result = new HashSet<String>();
        char c;
        for (int i = 0; i < input.length(); i++) {
            c = input.charAt(i);
            String remainder = input.substring(0, i) + input.substring(i+1);
            if (remainder.length() <= 1) {
                result.add(s + String.valueOf(c) + remainder);
            } else {
                Collection<String> other = permutations(s + String.valueOf(c), remainder);
                result.addAll(other);
            }
        }
        return result;
    }
    
    public static void main(String[] args) {
        Collection<String> result = permutations("", "abc");
        System.out.println(result.size());
        
        System.out.println(result);
        
        List<String> expected = Arrays.asList("abcd,abdc,acbd,acdb,adbc,adcb,badc,bacd,bcda,bcad,bdca,bdac,cabd,cadb,cbad,cbda,cdab,cdba,dacb,dabc,dbca,dbac,dcba,dcab".split(","));
        Set<String> expected2 = new HashSet<String>(expected);
        System.out.println(result.equals(expected2));
    }
    
}
